﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Content;

namespace RPGX.Graphics
{
    public class HUDObject_Image : HUDObject
    {
        #region Properties

        public override string Name
        {
            get
            {
                return "Image";
            }
        }

        public Texture2D Texture
        {
            get { return _texture; }
            set { _texture = value; }
        }
        private Texture2D _texture = null;

        #endregion

        #region Public Methods

        public override bool Render(GameTime time, SpriteBatch spriteBatch, Rectangle bounds)
        {
            if (this.Texture == null)
                return false;

            Rectangle pos = new Rectangle();
            pos.X = bounds.X + (int)this.PositionRelativeToParent.X;
            pos.Y = bounds.Y + (int)this.PositionRelativeToParent.Y;
            pos.Width = this.Width;
            pos.Height = this.Height;

            spriteBatch.Draw(this.Texture, pos, Color.White);

            foreach (var c in this.ChildObjects)
            {
                c.Render(time, spriteBatch, pos);
            }

            return true;
        }

        public override bool FromXML(XElement element, HUD hud)
        {
            this.ChildObjects = hud.LoadChildElements(element, this);

            string texture = element.Attribute("texture").Value;

            this.Texture = hud.Graphics.Game.Content.Load<Texture2D>(texture);

            return true;
        }

        public override bool UnLoad()
        {
            this.Texture.Dispose();
            this.Texture = null;

            return true;
        }

        #endregion
    }
}
